home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16_Src.lha / Python16_Source / Parser / myreadline.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-10  |  2.7 KB  |  124 lines

  1. /* Readline interface for tokenizer.c and [raw_]input() in bltinmodule.c.
  2.    By default, or when stdin is not a tty device, we have a super
  3.    simple my_readline function using fgets.
  4.    Optionally, we can use the GNU readline library.
  5.    my_readline() has a different return value from GNU readline():
  6.    - NULL if an interrupt occurred or if an error occurred
  7.    - a malloc'ed empty string if EOF was read
  8.    - a malloc'ed string ending in \n normally
  9. */
  10.  
  11. #include "Python.h"
  12. #include "protos/myreadline.h"
  13.  
  14. int (*PyOS_InputHook)() = NULL;
  15.  
  16. /* This function restarts a fgets() after an EINTR error occurred
  17.    except if PyOS_InterruptOccurred() returns true. */
  18.  
  19. static int
  20. my_fgets(buf, len, fp)
  21.     char *buf;
  22.     int len;
  23.     FILE *fp;
  24. {
  25.     char *p;
  26.     for (;;) {
  27.         if (PyOS_InputHook != NULL)
  28.             (void)(PyOS_InputHook)();
  29.         errno = 0;
  30.         p = fgets(buf, len, fp);
  31.         if (p != NULL)
  32.             return 0; /* No error */
  33.         if (feof(fp)) {
  34.             return -1; /* EOF */
  35.         }
  36. #ifdef EINTR
  37.         if (errno == EINTR) {
  38.             if (PyOS_InterruptOccurred()) {
  39.                 return 1; /* Interrupt */
  40.             }
  41.             continue;
  42.         }
  43. #endif
  44.         if (PyOS_InterruptOccurred()) {
  45.             return 1; /* Interrupt */
  46.         }
  47.         return -2; /* Error */
  48.     }
  49.     /* NOTREACHED */
  50. }
  51.  
  52.  
  53. /* Readline implementation using fgets() */
  54.  
  55. char *
  56. PyOS_StdioReadline(prompt)
  57.     char *prompt;
  58. {
  59.     int n;
  60.     char *p;
  61.     n = 100;
  62.     if ((p = PyMem_MALLOC(n)) == NULL)
  63.         return NULL;
  64.     fflush(stdout);
  65.     if (prompt)
  66.         fprintf(stderr, "%s", prompt);
  67.     fflush(stderr);
  68.     switch (my_fgets(p, n, stdin)) {
  69.     case 0: /* Normal case */
  70.         break;
  71.     case 1: /* Interrupt */
  72.         PyMem_FREE(p);
  73.         return NULL;
  74.     case -1: /* EOF */
  75.     case -2: /* Error */
  76.     default: /* Shouldn't happen */
  77.         *p = '\0';
  78.         break;
  79.     }
  80. #ifdef MPW
  81.     /* Hack for MPW C where the prompt comes right back in the input */
  82.     /* XXX (Actually this would be rather nice on most systems...) */
  83.     n = strlen(prompt);
  84.     if (strncmp(p, prompt, n) == 0)
  85.         memmove(p, p + n, strlen(p) - n + 1);
  86. #endif
  87.     n = strlen(p);
  88.     while (n > 0 && p[n-1] != '\n') {
  89.         int incr = n+2;
  90.         p = PyMem_REALLOC(p, n + incr);
  91.         if (p == NULL)
  92.             return NULL;
  93.         if (my_fgets(p+n, incr, stdin) != 0)
  94.             break;
  95.         n += strlen(p+n);
  96.     }
  97.     return PyMem_REALLOC(p, n+1);
  98. }
  99.  
  100.  
  101. /* By initializing this function pointer, systems embedding Python can
  102.    override the readline function.
  103.  
  104.    Note: Python expects in return a buffer allocated with PyMem_Malloc. */
  105.  
  106. char *(*PyOS_ReadlineFunctionPointer) Py_PROTO((char *));
  107.  
  108.  
  109. /* Interface used by tokenizer.c and bltinmodule.c */
  110.  
  111. char *
  112. PyOS_Readline(prompt)
  113.     char *prompt;
  114. {
  115.     char *rv;
  116.     if (PyOS_ReadlineFunctionPointer == NULL) {
  117.             PyOS_ReadlineFunctionPointer = PyOS_StdioReadline;
  118.     }
  119.     Py_BEGIN_ALLOW_THREADS
  120.     rv = (*PyOS_ReadlineFunctionPointer)(prompt);
  121.     Py_END_ALLOW_THREADS
  122.     return rv;
  123. }
  124.